package oracle.apps.financials.collection.listener;

import greendroid.widget.ItemAdapter;
import greendroid.widget.item.Item;
import greendroid.widget.item.SubtextItem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.po.Customer;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;

public class SearchTopDelinquentPopupWindowListener extends BaseOnClickListener {

	private static final String TAG = SearchTopDelinquentPopupWindowListener.class.getSimpleName();

	private String initialQuery;
	private EditText mSearchEdit;
	private ListView listView;
	private ItemAdapter adapter;
	private List<Customer> topDelinquentCustomerList;

	private View.OnKeyListener enterKeyHandler = new View.OnKeyListener() {
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
				if (event.getAction() == KeyEvent.ACTION_UP) {
					startSearch();
				}
				return true;
			}
			return false;
		}
	};

	public SearchTopDelinquentPopupWindowListener(Context context, List<Customer> customerList, Handler mHandler) {
		super(context, R.layout.top_delinquent_customer_search, null);
		this.mHandler = mHandler;
		this.topDelinquentCustomerList = customerList;
	}

	protected boolean startSearch() {
		initialQuery = mSearchEdit.getText().toString();
		Log.d(TAG, "The initialQuery:" + initialQuery);
		adapter.resetAdapter(getCustomerItemList(getCustomerListByName(topDelinquentCustomerList, initialQuery)));
		return false;
	}

	public List<Item> getCustomerItemList(List<Customer> customerList) {
		List<Item> items = new ArrayList<Item>();
		if (customerList != null) {
			Iterator<Customer> it = customerList.iterator();
			while (it.hasNext()) {
				Customer customer = it.next();
				SubtextItem subtextItem = new SubtextItem(customer.getCustomerName(), "Balance: " + customer.getCurrentMetricsItem().getNetBalance()
						+ "  Overdue: " + customer.getCurrentMetricsItem().getAccountOverdue());
				items.add(subtextItem);
			}
		}
		return items;
	}

	@Override
	public void onClick(View parent) {
		if (popupWindow == null) {
			intiPopupWindow();
		}
		popupWindow.showAsDropDown(parent, 15, 0);
	}

	public void intiPopupWindow() {
		this.popupWindow = new PopupWindow(popupWindowView, 250, 330);

		listView = (ListView) popupWindowView.findViewById(R.id.top_depliqent_customer_list);
		adapter = new ItemAdapter(context, getCustomerItemList(getCustomerListByName(topDelinquentCustomerList, initialQuery)));

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView listView = (ListView) parent;
				SubtextItem item = (SubtextItem) listView.getItemAtPosition(position);
				List<Customer> list = getCustomerListByName(topDelinquentCustomerList, item.text);
				if (!list.isEmpty()) {
					SearchTopDelinquentPopupWindowListener.this.notifyHandler(list.get(0));
					SearchTopDelinquentPopupWindowListener.this.dismissPopupWindow();
				}
			}
		});

		mSearchEdit = (EditText) this.popupWindowView.findViewById(R.id.search_edit);
		mSearchEdit.setOnKeyListener(enterKeyHandler);
		mSearchEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				startSearch();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

		});
		
		
		
//
//		ImageButton searchButton = (ImageButton) this.popupWindowView.findViewById(R.id.search_button);
//
//		searchButton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				startSearch();
//			}
//		});

		popupWindow.setTouchInterceptor(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					popupWindow.dismiss();
					return true;
				}
				return false;
			}

		});
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	public List<Customer> getCustomerListByName(List<Customer> customerList, String CustomerName) {

		List<Customer> list = new ArrayList<Customer>();

		if (TextUtils.isEmpty(CustomerName)) {
			list = customerList;
		} else {
			for (Customer customer : customerList) {
				if (customer.getCustomerName().toLowerCase().contains(CustomerName.toLowerCase())) {
					list.add(customer);
				}
			}
		}
		return list;
	}

}
